#include "DungeonsCacheData.h"

DungeonsCacheData* DungeonsCacheData::mInstance = NULL;

void DungeonsCacheData::CreateInstance()
{
	if (mInstance == nullptr)
	{
		mInstance = new DungeonsCacheData();
	}
}

DungeonsCacheData* DungeonsCacheData::GetInstance()
{
	return mInstance;
}

DungeonsCacheData::DungeonsCacheData()
	: m_maxCount(0)
{
}

DungeonsCacheData::~DungeonsCacheData()
{
	Clear();
}

bool DungeonsCacheData::Reload()
{
	_safe_guard_begin;

	Clear();

	Initialization();

	_safe_guard_end;

	return false;
}

void DungeonsCacheData::Clear()
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.begin();
	for (; itor != m_mapCacheData.end(); ++itor)
	{
		_safe_delete(itor->second);
	}

	m_mapCacheData.clear();

	_safe_guard_end;
}

bool DungeonsCacheData::Initialization()
{
	_safe_guard_begin;

	Json::Value _jv = ReadJsonFile("./data/map/dungeons.json");
	if (_jv == Json::nullValue)
	{
		LOG_ERROR("default","ReadJsonFile -> data/map/dungeons.json failed ......");
		return false;
	}

	m_maxCount = _jv.size();

	Json::Value::Members _tmp = _jv.getMemberNames();
	uint32_t _size = (uint32_t)(_tmp.size());
	for (uint32_t i = 0; i < _size; ++i)
	{
		uint32_t _idx = (uint32_t)atoi(_tmp[i].c_str());

		AddData(_idx, _jv[_tmp[i]]);
	}

	return true;

	_safe_guard_end;

	return false;
}


void DungeonsCacheData::AddData(uint32_t id, Json::Value& jm)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor == m_mapCacheData.end())
	{
		Dungeons* p = new Dungeons;
		_safe_check(p);

		p->mId = jm["map_id"].asUInt();
		strcpy(p->mName, jm["name"].asString().c_str());
		strcpy(p->mMapIcon, jm["map_icon"].asString().c_str());
		strcpy(p->mMapName, jm["map_name"].asString().c_str());
		strcpy(p->mInfo, jm["info"].asString().c_str());

		p->mTypes = jm["type"].asUInt();
		p->mBigMapId = jm["bigmap_id"].asUInt();
		p->mReleased = jm["released"].asUInt();
		p->mExp = jm["exp"].asUInt();
		p->mPowerCost = jm["power_cost"].asUInt();
		p->mPassCost = jm["pass_cost"].asUInt();
		p->mDropDouble = jm["drop_double"].asUInt();
		p->mTimeLimit = jm["time_limit"].asUInt();
		p->mPassLv = jm["pass_lv"].asUInt();
		p->mFightingCapacity = jm["fighting_capacity"].asUInt();
		p->mBossShow = jm["boss_show"].asUInt();

		// [[103000100,1,30],[103000200,1,10],[106000600,1,30]]
		Json::Value drop = jm["drop"];
		uint32_t sz = (uint32_t)(drop.size());
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = drop[i];
			uint32_t szz = (uint32_t)(vv.size());
			UInt32_3x c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}
			p->mDrop.push_back(c);
		}

		p->mGold = jm["gold"].asUInt();
		p->mDiamond = jm["diamond"].asUInt();
		p->mEliteId = jm["elite_id"].asUInt();

		// [[103000100,1,30],[103000200,1,10],[106000600,1,30]]
		Json::Value allMonster = jm["all_monster"];
		sz = allMonster.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = allMonster[i];
			uint32_t szz = vv.size();
			UInt32_2x c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}
			p->mAllMonster.push_back(c);
		}

		Json::Value treasureBox = jm["treasure_box"];
		sz = treasureBox.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mTreasureBox[i] = treasureBox[i].asUInt();
		}

		strcpy(p->mStar1Describe, jm["1star_describe"].asString().c_str());

		Json::Value star1Parameter = jm["1star_parameter"];
		sz = star1Parameter.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mStar1Parameter[i] = star1Parameter[i].asUInt();
		}

		strcpy(p->mStar2Describe, jm["2star_describe"].asString().c_str());

		Json::Value star2Parameter = jm["2star_parameter"];
		sz = star2Parameter.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mStar2Parameter[i] = star2Parameter[i].asUInt();
		}

		strcpy(p->mStar3Describe, jm["3star_describe"].asString().c_str());
		
		Json::Value star3Parameter = jm["3star_parameter"];
		sz = star3Parameter.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mStar3Parameter[i] = star3Parameter[i].asUInt();
		}

		m_mapCacheData[id] = p;
	}

	_safe_guard_end;
}

Dungeons* DungeonsCacheData::GetData(uint32_t id)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor != m_mapCacheData.end())
	{
		return itor->second;
	}
	_safe_guard_end;

	return nullptr;
}

bool DungeonsCacheData::GetDataJsonString(uint32_t id, Json::Value& _jm)
{
	_safe_guard_begin;

	Dungeons* p = GetData(id);
	if (nullptr == p)
	{
		LOG_ERROR("default","Error3");
		return false;
	}

	_jm.clear();

	_jm["id"] = Json::Value(p->mId);


	_safe_guard_end;

	return true;
}

uint32_t DungeonsCacheData::GetDataCount()
{
	_safe_guard_begin;

	return m_maxCount;

	_safe_guard_end;
}